package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.DetailReceipt;
import com.ruoyi.common.core.domain.entity.HeadReceipt;
import com.ruoyi.common.core.domain.entity.ReceiptFrom;
import com.ruoyi.system.mapper.DetailReceiptMapper;
import com.ruoyi.system.mapper.HeadReceiptMapper;
import com.ruoyi.system.mapper.InventoryMapper;
import com.ruoyi.system.mapper.PurchaseMapper;
import com.ruoyi.system.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 采购单 业务层处理
 *
 * @author kc
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    protected Validator validator;
    @Autowired
    private PurchaseMapper POMapper;
    @Autowired
    private HeadReceiptMapper HeadReceiptMapper;
    @Autowired
    private DetailReceiptMapper DRMapper;
    @Autowired
    private InventoryMapper ItMapper;

    /**
     * 采购头单查询
     */
    @Override
    public List<HeadReceipt> PurchaseHeadReceiptQuery(HeadReceipt bo) {
        return POMapper.PurchaseHeadReceiptQuery(bo);
    }

    /**
     * 采购明细查询
     */
    @Override
    public List<DetailReceipt> PurchaseDetailReceiptQuery(DetailReceipt bo) {
        return POMapper.PurchaseDetailReceiptQuery(bo);
    }

    /**
     * 采购单据ID查询
     */
    @Override
    public HeadReceipt selectPurchaseOrderById(String systematicReceipt) {
        return POMapper.selectPurchaseOrderById(systematicReceipt);
    }


    /**
     * 新增退库单据明细
     */
    private void addDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        DRMapper.addDetailReceipt(details);
        if (count == 0) {
            ItMapper.addInventory(details);
        } else {
            ItMapper.updateWarehousingInventory(details);
        }
    }

    /**
     * 新增出库单据明细
     */
    private void outboundDetails(List<DetailReceipt> details) {
        long count = ItMapper.selectInventoryById(details).size();
        if (count == 0) {
            throw new RuntimeException("货品明细中有货品库存为0，货品需入库后才能出库！");
        } else {
            DRMapper.addDetailReceipt(details);
            ItMapper.updateRetrievalInventory(details);
        }
    }

    /**
     * 修改库存单据明细
     */
    private void updateDetails(List<DetailReceipt> details) {
        DRMapper.delDetailReceipt(details);
        DRMapper.addDetailReceipt(details);
        ItMapper.updateWarehousingInventory(details);
        ItMapper.updateRetrievalInventory(details);
    }

    /**
     * 保存库存单据信息
     */
    @Override
    public int savePurchaseReceipt(ReceiptFrom bo) {
        long count = HeadReceiptMapper.selectHeadReceiptByCount(bo).size();
        List<DetailReceipt> details = bo.getDetails();
        if (count == 0) {
            if (bo.getReceiptType() == 1) {
                addDetails(details);
            } else if (bo.getReceiptType() == 2) {
                outboundDetails(details);
            } else {
                throw new RuntimeException("单据类型为空，请确认！");
            }
            return HeadReceiptMapper.addHeadReceipt(bo);
        } else {
            updateDetails(details);
            return HeadReceiptMapper.updateHeadReceipt(bo);
        }
    }

}
